﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using BaseSet.Entity.Common.DataOperation;

namespace WebSite.Admin.CashManager
{
    public partial class CashApplyList : BasePage
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadContent(false);

                // 从BasePage中得到
                PageLiteral.Text = BuildPageLinks();
            }
        }


        #region 加载列表
        private void LoadContent(bool mark)
        {

            //设置控件属性，从BasePage中得到
            PageConfig(CashApplyUPager);

            LoadCondition();

            int status = Convert.ToInt32(ddlStatus.SelectedValue);
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();

            //List<Member> list = new List<Member();
            CashApplyHelper cahelper = new CashApplyHelper();
            Criteria c = new Criteria();

            if (status != -1)
            {
                c.Add(CriteriaType.Equals, "Status", status);
            }
            if (begindate != "")
            {
                c.Add(CriteriaType.MoreThanEquals, "CreateDate", begindate);
            }
            if (enddate != "")
            {
                c.Add(CriteriaType.LessThanEquals, "CreateDate", enddate);
            }
            CashApplyUPager.ItemCount = cahelper.QueryCashApplyCountBySearch(c);

            // 以下的值从BasePage中得到
            CashApplyUPager.UrlFormat = UrlFormat;
            CashApplyUPager.PrefixText = GetPreFixText(CashApplyUPager);

            if (mark)
            {
                PagerInit(CashApplyUPager);
            }

            string[] fields = new string[] { "ID", "UsersID", "ApplyMoney", "Status", "Remark", "PayType", "PayContent", "PayDate", "CreateDate" };

            DataTable dt = new DataTable();
            dt = cahelper.QueryCashApplyBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, CashApplyUPager.Begin - 1, CashApplyUPager.Count, fields);

            dt.Columns.Add(new DataColumn("username"));
            dt.Columns.Add(new DataColumn("nickname"));
            dt.Columns.Add(new DataColumn("strstatus"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string usersid = dr["usersid"] + "";
                    //查找用户名和昵称
                    Users uinfo = new Users();
                    UsersHelper uhelper = new UsersHelper();
                    uinfo = uhelper.GetUsers(usersid);

                    dr["username"] = uinfo.UserName;
                    dr["nickname"] = uinfo.NickName;

                    //string paydate = dr["paydate"] + "";
                    //string strpaydate = "";
                    //if (!string.IsNullOrEmpty(paydate))
                    //{
                    //    strpaydate = Convert.ToDateTime(dr["paydate"]).ToString("yyyy-MM-dd HH:mm");
                    //}

                    //dr["strpaydate"] = strpaydate;

                    string getstatus = dr["status"] + "";
                    string strstatus = "";
                    if (getstatus == "0")
                    {
                        strstatus = "申请中";
                    }
                    else if (getstatus == "1")
                    {
                        strstatus = "已付款";
                    }
                    else if (getstatus == "2")
                    {
                        strstatus = "付款拒绝";
                    }

                    dr["strstatus"] = strstatus;
                }
            }

            rptCashApply.DataSource = dt;
            rptCashApply.DataBind();
        }
        #endregion

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            SetCondition();
            LoadContent(true);
        }

        #region 查询条件设置
        private void SetCondition()
        {
            string status = ddlStatus.SelectedValue;
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();

            //将查询条件放入Session中
            Session["QueryList"] =status+"@"+ begindate + "@" + enddate + "@" + "CashApplyList";

        }

        private void LoadCondition()
        {
            string condition = Session["QueryList"] + "";

            string[] conditionAll = condition.Split('@');
            if (conditionAll.Length == 4)
            {
                string page = conditionAll[3];
                if (page == "CashFlowList")
                {
                    ddlStatus.SelectedValue = conditionAll[0];
                    txtBeginDate.Text = conditionAll[1];
                    txtEndDate.Text = conditionAll[2];
                }
            }
        }
        #endregion

        #region 导出
        protected void btnExport_Click(object sender, EventArgs e)
        {
            string begindate = txtBeginDate.Text.Trim();
            string enddate = txtEndDate.Text.Trim();

            //List<Member> list = new List<Member();
            CashApplyHelper cahelper = new CashApplyHelper();
            Criteria c = new Criteria();

            c.Add(CriteriaType.Equals, "Status", 0);
            if (begindate != "")
            {
                c.Add(CriteriaType.MoreThanEquals, "CreateDate", begindate);
            }
            if (enddate != "")
            {
                c.Add(CriteriaType.LessThanEquals, "CreateDate", enddate);
            }

            string[] fields = new string[] { "ID", "UsersID", "ApplyMoney", "Status", "Remark", "PayType", "PayContent", "PayDate", "CreateDate" };

            DataTable dt = new DataTable();
            dt = cahelper.QueryCashApplyBySearchToTable(c, new Order[] { new Order("CreateDate") }, 0, 0, fields);

            dt.Columns.Add(new DataColumn("username"));
            dt.Columns.Add(new DataColumn("nickname"));
            dt.Columns.Add(new DataColumn("strcreatedate"));
            dt.Columns.Add(new DataColumn("strstatus"));
            dt.Columns.Add(new DataColumn("strpaydate"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string usersid = dr["usersid"] + "";
                    //查找用户名和昵称
                    Users uinfo = new Users();
                    UsersHelper uhelper = new UsersHelper();
                    uinfo = uhelper.GetUsers(usersid);

                    dr["username"] = uinfo.UserName;
                    dr["nickname"] = uinfo.NickName;

                    dr["strcreatedate"] = Convert.ToDateTime(dr["createdate"]).ToString("yyyy-MM-dd");

                    string paydate = dr["paydate"] + "";
                    string strpaydate = "";
                    if (!string.IsNullOrEmpty(paydate))
                    {
                        strpaydate = Convert.ToDateTime(dr["paydate"]).ToString("yyyy-MM-dd HH:mm");
                    }

                    dr["strpaydate"] = strpaydate;

                    string getstatus = dr["status"] + "";
                    string strstatus = "";
                    if (getstatus == "0")
                    {
                        strstatus = "申请中";
                    }
                    else if (getstatus == "1")
                    {
                        strstatus = "已付款";
                    }
                    else if (getstatus == "2")
                    {
                        strstatus = "付款拒绝";
                    }

                    dr["strstatus"] = strstatus;
                }

                string[] excelcolumnname = new string[] {"ID", "用户ID", "用户名", "昵称", "提现金额", "接收方式","账号信息","备注","状态", "申请日期","付款日期" };
                string[] fieldcolumn = new string[] { "ID", "UsersID", "username", "nickname", "applymoney", "PayType", "PayContent", "Remark", "strstatus", "strCreateDate", "strPayDate" };

                string filename = "UserCashApply_" + System.DateTime.Now;

                if (!FileOperation.ExportToExcel(Page, filename, dt, excelcolumnname, fieldcolumn, false))
                {
                    Tools.Alert("导出失败！");
                }
            }
            else
            {
                Tools.Alert("导出数据为空！");
            }
        }
        #endregion

        #region 列表事件
        protected void rptCashApply_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            HiddenField hidUsersID = (HiddenField)e.Item.FindControl("hidUsersID");
            HiddenField hidApplyMoney = (HiddenField)e.Item.FindControl("hidApplyMoney");

            string id = hidID.Value;
            string userid = hidUsersID.Value;
            int applymoney = Convert.ToInt32(hidApplyMoney.Value);

            if (e.CommandName == "ok")//已付款
            {
                CashApply cainfo = new CashApply();
                CashApplyHelper cahelper = new CashApplyHelper();
                cainfo = cahelper.GetCashApply(id);
                cainfo.Status = 1;
                cainfo.Remark = "提现成功";
                cainfo.PayDate = System.DateTime.Now;
                cahelper.EditCashApply(cainfo);

                //付款成功给用户发送消息
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                minfo.FromUsersID = "0";
                minfo.FromNickName = "系统";
                minfo.ToUsersID = userid;
                minfo.Flag = 0;
                minfo.Status = 0;
                minfo.MessageContent = "提现成功，金额已经打到提现账号中，请注意查收";
                mhelper.AddMessage(minfo);

                Tools.Alert("付款成功");
            }
            else if (e.CommandName == "no")//拒绝
            {
                CashApply cainfo = new CashApply();
                CashApplyHelper cahelper = new CashApplyHelper();
                cainfo = cahelper.GetCashApply(id);
                cainfo.Status = 2;
                cainfo.Remark = "提现拒绝";
                cainfo.PayDate = System.DateTime.Now;
                cahelper.EditCashApply(cainfo);

                //插入到现金流表中
                CashFlow cfinfo = new CashFlow();
                CashFlowHelper cfhelper = new CashFlowHelper();
                cfinfo.UsersID = userid;
                cfinfo.Money = applymoney;
                cfinfo.Type = 7;
                cfinfo.Description = "提现被拒绝" + applymoney.ToString();
                cfhelper.AddCashFlow(cfinfo);

                //付款成功给用户发送消息
                Message minfo = new Message();
                MessageHelper mhelper = new MessageHelper();
                minfo.FromUsersID = "0";
                minfo.FromNickName = "系统";
                minfo.ToUsersID = userid;
                minfo.Flag = 0;
                minfo.Status = 0;
                minfo.MessageContent = "提现被拒绝";
                mhelper.AddMessage(minfo);

                Tools.Alert("拒绝成功");
            }
            LoadContent(false);
        }
        #endregion
    }
}